{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from hydra import initialize, initialize_config_module, initialize_config_dir, compose\n",
    "from omegaconf import OmegaConf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Initializing Hydra\n",
    "There are several ways to initialize. See the [API docs](https://hydra.cc/docs/next/advanced/compose_api/#api-documentation) for full details.\n",
    "All methods support both a function call style that changes the global state, and a context style that cleans up when the scope exits.\n",
    "\n",
    "## initialize()\n",
    "Initializes Hydra and add the config_path to the config search path.\n",
    "config_path is relative to the parent of the caller, in this case it is realtive to the directory containing\n",
    "this Notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'db': {'driver': 'mysql', 'user': '???', 'pass': '???'}}\n"
     ]
    }
   ],
   "source": [
    "with initialize(config_path=\"cloud_app/conf\"):\n",
    "    cfg = compose(overrides=[\"+db=mysql\"])\n",
    "    print(cfg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## initialize_config_module()\n",
    "Initializes Hydra and add the config_module to the config search path.  \n",
    "The config module must be importable (an `__init__.py` must exist at its top level)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'db': {'driver': 'mysql', 'user': '???', 'pass': '???'}}\n"
     ]
    }
   ],
   "source": [
    "with initialize_config_module(config_module=\"cloud_app.conf\"):\n",
    "    cfg = compose(overrides=[\"+db=mysql\"])\n",
    "    print(cfg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## initialize_config_dir()\n",
    "Initializes Hydra and add the config_path to the config search path.\n",
    "The config_path must be an absolute path on the file system."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'db': {'driver': 'mysql', 'user': '???', 'pass': '???'}}\n"
     ]
    }
   ],
   "source": [
    "abs_config_dir=os.path.abspath(\"cloud_app/conf\")\n",
    "with initialize_config_dir(config_dir=abs_config_dir):\n",
    "    cfg = compose(overrides=[\"+db=mysql\"])\n",
    "    print(cfg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Global initialization\n",
    "Calling each of the initilizaiton methods outside of a context changes the global state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'db': {'driver': 'mysql', 'user': '???', 'pass': '???'}}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "initialize(config_path=\"cloud_app/conf\")\n",
    "compose(overrides=[\"+db=mysql\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Config composition\n",
    "Below are some more interesting demonstrations of config composition\n",
    "## Config Structure\n",
    "The `__init__.py` file is needed is needed to help Python find the config files in some scenarios (It can be empty).  \n",
    "```\n",
    "conf/\n",
    "├── application\n",
    "│   ├── bananas.yaml\n",
    "│   └── donkey.yaml\n",
    "├── cloud_provider\n",
    "│   ├── aws.yaml\n",
    "│   └── local.yaml\n",
    "├── db\n",
    "│   ├── mysql.yaml\n",
    "│   └── sqlite.yaml\n",
    "├── environment\n",
    "│   ├── production.yaml\n",
    "│   └── testing.yaml\n",
    "├── config.yaml\n",
    "└── __init__.py\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "db:\n",
      "  driver: mysql\n",
      "  user: mysql\n",
      "  pass: r4Zn*jQ9JB1Rz2kfz\n",
      "donkey:\n",
      "  name: kong\n",
      "  rank: king\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# compose application specific config (in this case the applicaiton is \"donkey\")\n",
    "cfg=compose(overrides= [\"+db=mysql\", \"+environment=production\", \"+application=donkey\"])\n",
    "print(OmegaConf.to_yaml(cfg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "db:\n",
      "  driver: sqlite\n",
      "  user: test\n",
      "  pass: test\n",
      "  file: test.db\n",
      "cloud:\n",
      "  name: local\n",
      "  host: localhost\n",
      "  port: 9876\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# compose from config.yaml, this composes a bunch of defaults in:\n",
    "cfg=compose(config_name=\"config.yaml\")\n",
    "print(OmegaConf.to_yaml(cfg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "db:\n",
      "  driver: sqlite\n",
      "  user: mysql\n",
      "  pass: r4Zn*jQ9JB1Rz2kfz\n",
      "  file: test.db\n",
      "cloud:\n",
      "  name: aws\n",
      "  api_key: __SOMETHING_FROM_AN_ENV_VARIABLE__\n",
      "  ami_id: MY_AMI_ID\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"AWS_API_KEY\"] = \"__SOMETHING_FROM_AN_ENV_VARIABLE__\"\n",
    "\n",
    "# compose from config.yaml and override from the default testing\n",
    "# environment to production and cloud provider to aws.\n",
    "# Also override the ami id\n",
    "\n",
    "cfg=compose(\n",
    "    config_name=\"config.yaml\", \n",
    "    overrides=[\n",
    "        \"cloud_provider=aws\",\n",
    "        \"environment=production\",\n",
    "        \"cloud.ami_id=MY_AMI_ID\",\n",
    "    ]\n",
    ")\n",
    "print(OmegaConf.to_yaml(cfg, resolve=True))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}